﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Xml;
using System.IO;
namespace FBProductsSynchronization
{
    public class Config
    {
        public static String key = "";
        public static ConnectionObject connectionObject = new ConnectionObject();
        public void start()
        {
            // XML login string
            String loginCommand = ConnectionObject.createLoginXml("admin", "fishbowl");
            Console.Write("Client Started... \n");
            string returnCommand = connectionObject.sendCommand(loginCommand);
            key = pullKey(returnCommand);
            if (key == "null")
            {
                Console.Write("Please accept the connection attempt on the fisbowl server and press return");
                Console.ReadLine();
                key = pullKey(connectionObject.sendCommand(loginCommand));
            }
        }
        
        public String pullKey(String connection)
        {
            String key = "";
            using (XmlReader reader = XmlReader.Create(new StringReader(connection)))
            {
                while (reader.Read())
                {
                    //if (reader.NodeType == XmlNodeType.Element && reader.Name.Equals("Key"))
                    if (reader.Name.Equals("Key") && reader.Read())
                    {
                        return reader.Value.ToString();
                    }
                }
            }
            return key;
        }
  
        public string ReturnKey()
        {
            return key;
        }
        public ConnectionObject ReturnConnection()
        {
            return connectionObject;
        }
    }
}
